<script setup lang="ts">
import { ref, computed, onMounted, nextTick } from 'vue'
import { ElMessage, ElDialog, ElSelect, ElOption, ElSwitch, ElAvatar } from 'element-plus'
import { Setting, Upload, ArrowUp as Top } from '@element-plus/icons-vue'
import { useChatStore } from '@/stores/chat'
import {storeToRefs} from 'pinia'
const chatStore = useChatStore()

// 使用storeToRefs解构响应式状态
const {
  messages,
  hasMessages,
  selectedModel,
  isLoading
} = storeToRefs(chatStore)

// 解构方法
const {
  sendMessage: storeSendMessage,
  startNewChat,
  switchModel,
  loadConversations
} = chatStore

const inputMessage = ref<string>('')
const showSettings = ref(false)
const multiModal = ref(false)

const sendMessage = async () => {
  if (inputMessage.value.trim() && !isLoading.value) {
    await storeSendMessage(inputMessage.value)
    inputMessage.value = ''
    await scrollToBottom()
  }
}

const scrollToBottom = async () => {
  await nextTick()
  const container = document.querySelector('.messages-container') as HTMLElement
  if (container) {
    container.scrollTop = container.scrollHeight
  }
}

const handleUpload = () => {
  ElMessage.info('文件上传功能：后续实现')
}

const updateModel = (model: 'deepseek' | 'kimi') => {
  switchModel(model)
}

const startDialog = async () => {
  inputMessage.value = '你好'
  await sendMessage()
  await scrollToBottom()
}

onMounted(() => {
  // 页面挂载时加载用户会话列表（实际应用中应从用户存储获取真实userId）
  const userId = 1
  loadConversations(userId)
})
</script>
<template>
  <div class="home-container">
    <div class="main-content">
      <!-- 无聊天记录时居中居中显示欢迎和输入框 -->
      <div v-if="!hasMessages" class="center-content">
        <div class="welcome-section">
          <h1>欢迎使用 Chainlit Llamaindex</h1>
          <p>开始新的对话，探索无限可能</p>
          <div class="input-container center-input">
            <textarea
              v-model="inputMessage"
              placeholder="请输入消息..."
              class="message-input"
              @keyup.enter="sendMessage"
            ></textarea>
            <div class="input-tools">
              <el-icon class="tool-icon" @click="showSettings = true"><Setting /></el-icon>
              <el-icon class="tool-icon" @click="handleUpload"><Upload /></el-icon>
            </div>
            <div class="send-button" @click="sendMessage">
              <el-icon class="send-icon"><Top /></el-icon>
            </div>
          </div>
          <el-button
            type="primary"
            size="large"
            @click="startDialog"
          >
            开始对话
          </el-button>
        </div>
      </div>

      <!-- 有聊天记录时 -->
      <div v-else class="chat-content">
        <div class="messages-container" ref="messagesContainer">
          <div
            v-for="message in messages"
            :key="message.id"
            :class="['message-item', message.role]"
          >
            <div class="message-avatar">
              <el-avatar :icon="message.role === 'user' ? 'User' : 'Promotion'" />
            </div>
            <div class="message-content">
              <div class="message-text">{{ message.content }}</div>
            </div>
          </div>
        </div>

        <div class="input-container bottom-input">
          <textarea
            v-model="inputMessage"
            placeholder="请输入消息..."
            class="message-input"
            @keyup.enter="sendMessage"
            :disabled="isLoading"
          ></textarea>
          <div class="input-tools">
            <el-icon class="tool-icon" @click="showSettings = true"><Setting /></el-icon>
            <el-icon class="tool-icon" @click="handleUpload"><Upload /></el-icon>
          </div>
          <div class="send-button" @click="sendMessage" :disabled="isLoading">
            <el-icon class="send-icon"><Top/></el-icon>
          </div>
        </div>
      </div>
    </div>

    <!-- 设置对话框 -->
    <el-dialog v-model="showSettings" title="设置" width="30%">
      <el-form>
        <el-form-item label="模型切换">
          <el-select v-model="selectedModel" @change="updateModel">
            <el-option label="Deepseek" value="deepseek" />
            <el-option label="Kimi" value="kimi" />
          </el-select>
        </el-form-item>
        <el-form-item label="多模态">
          <el-switch v-model="multiModal" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showSettings = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>



<style scoped>
.home-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  position: relative;
}

.center-content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.welcome-section {
  text-align: center;
  max-width: 1000px;
}

.welcome-section h1 {
  font-size: 32px;
  color: #1f2937;
  margin-bottom: 16px;
}

.welcome-section p {
  font-size: 16px;
  color: #6b7280;
  margin-bottom: 32px;
}

.chat-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.message-item {
  display: flex;
  margin-bottom: 20px;
  max-width: 80%;
}

.message-item.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-item.assistant {
  align-self: flex-start;
}

.message-avatar {
  flex-shrink: 0;
  margin: 0 12px;
}

.message-content {
  flex: 1;
}

.message-text {
  padding: 12px 16px;
  border-radius: 20px;
  line-height: 1.5;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.message-item.user .message-text {
  background-color: #3b82f6;
  color: white;
}

.message-item.assistant .message-text {
  background-color: white;
  color: #374151;
  border: 1px solid #e5e7eb;
}

.input-container {
  position: relative;
  padding: 30px;
  background-color: white;
  border-radius: 24px;
  box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.08);
  margin: 0 auto;
  max-width: 1000px;
  width: 100%;
  box-sizing: border-box;
  min-height: 120px;
}

.center-input {
  margin-bottom: 20px;
  width: 100%;
  max-width: 1000px;
}

.bottom-input {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
}

.message-input {
  width: 100%;
  height: 100px;
  border-radius: 20px;
  background-color: #f9fafb;
  border: none;
  padding: 12px 60px 40px 20px;
  font-size: 16px;
  resize: none;
  outline: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
}

.input-tools {
  position: absolute;
  bottom: 12px;
  left: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.tool-icon {
  cursor: pointer;
  font-size: 20px;
  color: #6b7280;
  margin-right: 8px;
}

.send-button {
  position: absolute;
  bottom: 12px;
  right: 10px;
  width: 30px;
  height: 30px;
  background-color: #3b82f6;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.send-icon {
  font-size: 20px;
  color: white;
}
</style>